import { ElMessage } from "element-plus";
/**
 * @namne 基础请求前拦截中间件
 */
export const baseRequest = {
  fulfilled: (config) => {
    return config;
  },
  rejected: (error) => {
    return Promise.reject(error);
  },
};

/**
 * @namne 基础响应后拦截中间件
 */
export const baseResponse = {
  fulfilled: (response, next) => {
    try {
      const status = response.status;
      const config = response.config;
      const customConfig = config.customConfig;
      const data = response.data;
      const code = data?.code;
      const message = data?.message || data?.msg;
      if (status === 200) {
        if (code === 0 || code === 200) {
          return Promise.resolve(data);
        }
        // 特殊接口没有code
        if (customConfig.not_code) {
          return Promise.resolve(data);
        }
        if (customConfig.show_code_error) {
          ElMessage.error(code === -1 ? message : customConfig?.error_msg || message);
        }
        return Promise.reject(data);
      } else {
        if (customConfig.auto_show_error_message) {
          ElMessage.error(customConfig?.error_msg || message || "接口异常");
        }
        return Promise.reject(data);
      }
      next();
    } catch (e) {
      return Promise.reject(e);
    }
  },
  rejected: (error) => {
    let message = "";
    if (error.message.includes("timeout")) {
      message = "网络请求超时！";
    }
    if (error.message.includes("Network")) {
      message = window.navigator.onLine ? "服务端异常！" : "您断网了！";
    }
    ElMessage({
      type: "error",
      message,
    });
    return Promise.reject(error);
  },
};

/**
 * @namne 下载请求前拦截中间件
 */
export const downRequest = (config, next) => {
  if (config.customConfig?.auto_download) {
    config.responseType = "blob";
  }
  next();
};

/**
 * @namne 下载响应后拦截中间件
 */
export const downResponse = (response, next) => {
  const config = response.config;
  const customConfig = config.customConfig;
  // 如果是下载文件
  if (customConfig.auto_download) {
    let name = "";
    if (response.headers["content-disposition"]) {
      name = response.headers["content-disposition"]
        .split(";")[1]
        .split("=")[1]
        .replace(/^\"|\"$/g, "");
    } else {
      // 转码弹消息
      let enc = new TextDecoder("utf-8");
      const tableData = JSON.parse(enc.decode(new Uint8Array(response.data)));
      if (response?.notSuccess) {
        ElMessage.error(tableData.msg || "下载错误");
        return;
      }
      return Promise.reject(response.data);
    }
    // 这里res.data是返回的blob对象
    let blob = new Blob([response.data], {
      type: customConfig?.down_type || "application/vnd.ms-excel",
    });
    let downloadElement = document.createElement("a");
    let href = window.URL.createObjectURL(blob); // 创建下载的链接
    downloadElement.href = href;
    downloadElement.download = decodeURI(name); // 下载后文件名
    document.body.appendChild(downloadElement);
    downloadElement.click(); // 点击下载
    document.body.removeChild(downloadElement); // 下载完成移除元素
    window.URL.revokeObjectURL(href);
    return Promise.resolve(response.data);
  }
  next();
};
